package com.xdcplus.web.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.exceptions.XdcWebException;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.sys.SysDepartmentDTO;
import com.xdcplus.web.common.pojo.dto.sys.SysDepartmentFilterDTO;
import com.xdcplus.web.common.pojo.entity.sys.SysDepartment;
import com.xdcplus.web.common.pojo.query.sys.SysDepartmentQuery;
import com.xdcplus.web.common.pojo.vo.sys.SysDepartmentVO;
import com.xdcplus.web.mapper.sys.SysDepartmentMapper;
import com.xdcplus.web.service.sys.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

/**
 * 部门(SysDepartment)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-06-18 14:00:40
 */
@Slf4j
@Service("sysDepartmentService")
public class SysDepartmentServiceImpl extends BaseServiceImpl<SysDepartmentMapper, SysDepartment, SysDepartment, SysDepartmentVO> implements SysDepartmentService {

    @Autowired
    protected SysDepartmentMapper sysDepartmentMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveSysDepartment(SysDepartmentDTO sysDepartmentDTO) {

        SysDepartment sysDepartment = new SysDepartment();
        BeanUtil.copyProperties(sysDepartmentDTO, sysDepartment);
        sysDepartment.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(sysDepartment);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateSysDepartment(SysDepartmentDTO sysDepartmentDTO) {

        SysDepartment sysDepartment = this.getById(sysDepartmentDTO.getId());
        if (ObjectUtil.isNull(sysDepartment)) {
            log.error("updateSysDepartment() The SysDepartment does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(sysDepartmentDTO, sysDepartment);
        sysDepartment.setUpdatedUser(sysDepartmentDTO.getUpdatedUser());
        sysDepartment.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(sysDepartment);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<SysDepartment> sysDepartmentList) {

        sysDepartmentList.forEach(sysDepartment -> {
            SysDepartment sysDepartmentParam = new SysDepartment();
            sysDepartmentParam.setId(sysDepartment.getId());
            if (ObjectUtil.isNotNull(sysDepartment.getId())) {
                sysDepartment.setId(sysDepartment.getId());
                sysDepartment.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<SysDepartment> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(SysDepartment::getId, sysDepartment.getId());
                update(sysDepartment, lambdaUpdate);
            } else {
                sysDepartment.setCreatedTime(new Timestamp(DateUtil.current()));
                save(sysDepartment);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<SysDepartmentDTO> sysDepartmentDTOList) {

        List<SysDepartment> sysDepartmentList = BeanUtils.copyProperties(sysDepartmentDTOList, SysDepartment.class);
        return saveOrUpdateBatch(sysDepartmentList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteSysDepartmentLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        SysDepartment sysDepartment = this.getById(id);

        if (ObjectUtil.isNull(sysDepartment)) {
            log.error("deleteSysDepartment() The SysDepartment does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteSysDepartmentByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			SysDepartment sysDepartment = this.getById(id);
			if (ObjectUtil.isNull(sysDepartment)) {
				log.error("deleteSysDepartment() The SysDepartment does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<SysDepartment> querySysDepartmentList(SysDepartmentFilterDTO sysDepartmentFilterDTO) {
        SysDepartmentQuery sysDepartmentQuery = BeanUtil.copyProperties(sysDepartmentFilterDTO, SysDepartmentQuery.class);

        return sysDepartmentMapper.querySysDepartment(sysDepartmentQuery);
    }

    @Override
    public List<SysDepartmentVO> querySysDepartmentVOList(SysDepartmentFilterDTO sysDepartmentFilterDTO) {
        return this.objectConversion(querySysDepartmentList(sysDepartmentFilterDTO));
    }

    @Override
    public PageVO<SysDepartmentVO> querySysDepartment(SysDepartmentFilterDTO sysDepartmentFilterDTO) {
        PageVO<SysDepartmentVO> pageVO = new PageVO<>();

        if (sysDepartmentFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(sysDepartmentFilterDTO.getCurrentPage(), sysDepartmentFilterDTO.getPageSize(),
                    sysDepartmentFilterDTO.getOrderType(), sysDepartmentFilterDTO.getOrderField());
        }

        List<SysDepartment> sysDepartmentList = querySysDepartmentList(sysDepartmentFilterDTO);

        PageInfo<SysDepartment> pageInfo = new PageInfo<>(sysDepartmentList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(sysDepartmentList));

        return pageVO;
    }

    @Override
    public SysDepartmentVO querySysDepartmentById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public SysDepartment querySysDepartmentByName(String name) {
        return sysDepartmentMapper.querySysDepartmentByName(name);
    }
}
